package com.shanyue.redwine.pay.weixin;

import android.content.Context;
import android.os.AsyncTask;

import com.shanyue.redwine.utils.Constants;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 必传6个参数.1234参数不为空,1:appid,2:商户id;3:api_key;4:是否已经生成预支付订单,"00",否,"01",已生成预支付订单;5:预支付订单好;6:通知url
 * Created by Administrator on 2015/12/2.
 */
public class WXpayAsyncTask extends AsyncTask<String, Integer, String> {
    private static final String TAG = WXpayAsyncTask.class.getName();
    private IWXAPI api;
    Context context;
    WeChatPayInfo payReq;

    public WXpayAsyncTask(Context context, WeChatPayInfo payReq) {
        this.api = WXAPIFactory.createWXAPI(context, Constants.PAY_WEIXIN_APPID);
        api.registerApp(Constants.PAY_WEIXIN_APPID);
        this.context = context;
        this.payReq = payReq;
    }

    @Override
    protected String doInBackground(String... params) {
        PayReq req = new PayReq();
        req.appId = payReq.getAppId();
        req.partnerId = payReq.getPartnerId();
        req.prepayId = payReq.getPrepayId();
        req.nonceStr = payReq.getNonceStr();
        req.timeStamp = payReq.getTimeStamp();
        req.packageValue = payReq.getPackageValue();
        req.sign = payReq.getSign();
        req.extData = "app data"; // optional
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
        return null;
    }


}
